﻿using DaVikingCode.AssetPacker;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class AssetPackerExample : MonoBehaviour {
	
	public Image anim;
    public Image image1;
    public Image image2;
    public Text msg;

	AssetPacker assetPacker;

    void Start() {

        this.assetPacker = new AssetPacker { useCache = true, cacheName = "w", cacheVersion = 2 };

        for (int i = 1; i < 10; ++i)
        {
            string file = "/Sprites/Textures/walking000" + i + ".png";
            StartCoroutine(CopyFileFromSAPathToPDPath(i, file, null, loaded));
        }
        for(int i = 10; i < 12; ++i)
        {
            string file = "/Sprites/Textures/walking00" + i + ".png";
            StartCoroutine(CopyFileFromSAPathToPDPath(i, file, null, loaded));
        }
    }

    private void loaded(int index)
    {
        this.msg.text = "loaded index = " + index;
        if(index == 11)
        {
            string[] files = Directory.GetFiles(Application.persistentDataPath + "/Sprites/Textures", "*.png");

            assetPacker.OnProcessCompleted += LaunchAnimations;
            assetPacker.AddTexturesToPack(files);
            assetPacker.Process("Walk", 2048);
        }
    }

	void LaunchAnimations(bool isSuccess, string msg) {
        this.msg.text = msg;

        if(isSuccess)
            StartCoroutine(LoadAnimation());
    }

	IEnumerator LoadAnimation() {
		SpriteInfo[] sis = assetPacker.GetSprites("walking");

		int j = 0;
		while (j < sis.Length) {

			anim.sprite = sis[j++].sprite;

            image1.sprite = sis[(j + 4) % sis.Length].sprite;
            image2.sprite = sis[(j + 8) % sis.Length].sprite;

			yield return new WaitForSeconds(0.1f);

			if (j == sis.Length)
				j = 0;
		}
	}

    //将SteamingAssets目录下的文件拷贝到PersistentDataPath下
    public IEnumerator CopyFileFromSAPathToPDPath(int index, string InSAName, string InPDPName = null, Action<int> InOnCopyFinishedAction = null)
    {
        UnityEngine.Assertions.Assert.IsFalse(string.IsNullOrEmpty(InSAName));
        if (string.IsNullOrEmpty(InPDPName)) InPDPName = InSAName;
        string streamingAssetPath = GetStreamingAssetsPath() + InSAName;
        string persistentDataPath = Application.persistentDataPath + InPDPName;

        var dirPath = persistentDataPath.Substring(0, persistentDataPath.LastIndexOf("/"));
        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }

        if(!File.Exists(persistentDataPath))
        {
            this.msg.text = "enter index = " + index;
#if UNITY_ANDROID
            WWW www = new WWW(streamingAssetPath);
            yield return www;

            if (www.isDone && string.IsNullOrEmpty(www.error))
            {
                File.WriteAllBytes(persistentDataPath, www.bytes);
                InOnCopyFinishedAction.Invoke(index);
            }
            else
                Debug.LogError("[CopyFileFromSAPathToPDPath] error:" + www.error);
#else
        File.Copy(streamingAssetPath, persistentDataPath, true);
#endif
        }
        else
        {
            InOnCopyFinishedAction.Invoke(index);
        }

        yield return null;
    }

    public string GetStreamingAssetsPath()
    {
#if UNITY_ANDROID
        return Application.streamingAssetsPath; //android比较特殊
#else
        return "file://" + Application.streamingAssetsPath; //pc和iOS可用
#endif
    }
}
